import Vue from 'vue'
import Vuex from 'vuex'
import ajax from '../utils/ajax/index'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    loading: false, // 加载
    breadcrumb: null, // 面包屑内容
    table: [], // 表头
    tableData: [], // 表格数据
    page: {}, // 分页数据
    projectList: [], // 项目列表
    cityMsg: [], // 项目列表
    factoryMsg: [], // 项目列表
    productMsg: [], // 项目列表
    repairMsg: [] // 项目列表
  },
  mutations: {
    loading (state, data) {
      state.loading = data
    },
    breadcrumb (state, arr) {
      state.breadcrumb = arr
    },
    table (state, data) {
      state.table = data
    },
    tableData (state, data) {
      state.tableData = data
    },
    setPage (state, data) {
      state.page = data
    },
    setClearPage (state, data) {
      state.page = {}
    },
    setProjectList (state, data) {
      state.projectList = data
    },
    setCityMsg (state, data) {
      state.cityMsg = data
    },
    setFactoryMsg (state, data) {
      state.factoryMsg = data
    },
    setProductMsg (state, data) {
      state.productMsg = data
    },
    setRepairMsg (state, data) {
      state.repairMsg = data
    }
  },
  actions: {
    setLoading ({commit}, data) {
      commit('loading', data)
    },
    setBreadcrumb ({commit}, arr) {
      commit('breadcrumb', arr)
    },
    setTable ({commit}, data) { // 写入表头
      commit('table', data)
    },
    setTableData ({commit}, data) { // 写入表格数据
      commit('tableData', data)
    },
    setPage ({commit}, data) { // 写入分页数据
      commit('page', data)
    },
    setClearPage ({commit}) { // 清空分页数据
      commit('clearPage')
    },
    getProjectList ({state, commit, dispatch}, obj) { // 获取项目列表
      ajax.get(obj.url, {}).then((res) => {
        let data = res.data
        if (res.msg.code === '20000') {
          let temp = []
          for (let i = 0; i < data.length; i++) {
            let obj = {}
            obj.id = data[i].project_code
            obj.name = data[i].project_name
            obj.time = data[i].startUpTime
            obj.num = data[i].count
            obj.cityName = data[i].city_code
            obj.factoryName = data[i].factory_name
            obj.projectDutyName = data[i].duty
            temp.push(obj)
          }
          if (temp.length === 0) {
            obj.msg({message: '暂时没有相关项目详情', type: 'warning'})
          }
          commit('setProjectList', temp)
        }
      })
    },
    getCityMsg ({state, commit, dispatch}, obj) { // 获取城市列表
      ajax.get(obj.url, {}).then((res) => {
        if (res.msg.code === '20000') {
          commit('setCityMsg', res.data.list)
        } else {
          obj.msg.error(res.msg.message)
        }
      })
    },
    getFactoryMsg ({state, commit, dispatch}, obj) { // 获取厂商列表
      ajax.get(obj.url, {}).then((res) => {
        if (res.msg.code === '20000') {
          commit('setFactoryMsg', res.data.list)
        } else {
          obj.msg.error(res.msg.message)
        }
      })
    },
    getProductMsg ({state, commit, dispatch}, obj) { // 获取产品列表
      ajax.get(obj.url, {}).then((res) => {
        if (res.msg.code === '20000') {
          commit('setProductMsg', res.data.list)
        } else {
          obj.msg.error(res.msg.message)
        }
      })
    },
    getRepairMsg ({state, commit, dispatch}, obj) { // 获取产品型号列表
      ajax.get(obj.url, {}).then((res) => {
        if (res.msg.code === '20000') {
          commit('setRepairMsg', res.data.list)
        } else {
          obj.msg.error(res.msg.message)
        }
      })
    }
  }
})
